//
//  AirplaneDetailVC.swift
//  ImGuider X
//
//  Created by llt on 2018/9/27.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit

class AirplaneDetailVC: BaseVC {
    
    @IBOutlet weak var tableView: UITableView!
    var headerImageView : UIImageView!
    var headerView : UIView!
//    var flightView : FlightView!
    
    var viewmodel = AirplaneDetailViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setup()
    }
}


extension AirplaneDetailVC {
    
    func setup() {
        
        setNavBarStyle(.whiteAlpha(0))
        
        self.headerView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenWidth / 4 * 3))
        self.headerImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height:kScreenWidth / 4 * 3))
        self.headerImageView.backgroundColor = UIColor.blue
        self.headerImageView.image = UIImage(named: "flightBg")
        self.headerView.addSubview(self.headerImageView!)
        
//        flightView = FlightView()
//        self.headerView.addSubview(flightView)
//        
//        flightView.snp.makeConstraints {
//            
//            [weak self] make in
//            guard let `self` = self else { return }
//            
//            make.edges.equalTo(self.headerImageView)
//        }
        
        self.tableView.tableHeaderView = self.headerView
        
        let conerView = UIView(frame: CGRect.zero)
        
        conerView.backgroundColor = kThemeBgColor
        conerView.clipsToBounds = true
        conerView.layer.cornerRadius = 20
//                self.headerView?.clipsToBounds = true
        self.headerView?.addSubview(conerView)
        
        conerView.snp.makeConstraints {
            [weak self] maker in
            guard let `self` = self else { return }
            
            maker.top.equalTo(self.headerView!.snp.bottom).offset(-20)
            maker.left.equalToSuperview()
            maker.right.equalToSuperview()
            maker.bottom.equalTo(self.headerView!.snp.bottom).offset(20)
        }
        
        self.tableView.backgroundColor = kThemeBgColor
        self.tableView.register(UINib(nibName: "AirplaneCell", bundle: nil), forCellReuseIdentifier: "AirplaneCell")
        self.tableView.register(UINib(nibName: "FlightInforCell", bundle: nil), forCellReuseIdentifier: "FlightInforCell")
        
        self.tableView.rowHeight = UITableView.automaticDimension
        self.tableView.estimatedRowHeight = 100
       
        self.closeAutomaticallyInsets(scrollview: tableView)
        
//        self.flightView.configData(model: viewmodel.calender?.airplaneDetail)
        
    }
}


extension AirplaneDetailVC: UITableViewDelegate,UITableViewDataSource {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let yOffset = scrollView.contentOffset.y;
        let H = CGFloat(kScreenWidth/4 * 3)
        
        if (yOffset <= 0) {
            
            let factor = -yOffset + H
            
            let f = CGRect(x: -(CGFloat(kScreenWidth) * factor/H - CGFloat(kScreenWidth))/2,
                           y: yOffset,
                           width: CGFloat(kScreenWidth) * factor/H,
                           height: factor)
            self.headerImageView!.frame = f;
        }
        
        let alpha = yOffset / (H - CGFloat(kNavigationBarHeight / 2))
        
        self.setNavBarStyle(.whiteAlpha(alpha))
    }
    
    func  numberOfSections(in tableView: UITableView) -> Int {
        
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if section == 0 {
            return 1
        }
        
        return viewmodel.dataSource?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 0 {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "AirplaneCell") as! AirplaneCell
            
            cell.configData(model: viewmodel.calender?.airplaneDetail)
            
            return cell
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "FlightInforCell") as! FlightInforCell
        
        cell.openMaps = {
            
            [weak self] coor in
            guard let `self` = self else { return }
            
            if let coor = coor {
                
                Util.tractfic(to: coor, vc: self)
            }
        }
        
        let model = viewmodel.dataSource?[indexPath.row]

        var type = FlightType.transfer

        if indexPath.row == 0 {

            type = .departure
        } else if indexPath.row == ((viewmodel.dataSource?.count ?? 0) - 1) {

            type = .destination
        }
        
        cell.configData(airDetail: viewmodel.calender?.airplaneDetail, model: model?["air"], type: type)

        return cell
    }
    
}

